﻿using System.Collections;
using C5;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Extensions;

namespace Boggle
{
    public class BoggleTraversal : IEnumerable<BoggleCell>
    {
        /// <summary>
        /// stores cells that have been visited. Supports constant time hashset operations with a linked list backing for ordering.
        /// </summary>
        private readonly HashedLinkedList<BoggleCell> _visitedCells = new HashedLinkedList<BoggleCell>();

        public BoggleTraversal()
        {

        }

        public BoggleTraversal(BoggleTraversal traversal)
        {
            _visitedCells.AddAll(traversal);
        }

        public IEnumerator<BoggleCell> GetEnumerator()
        {
            return _visitedCells.GetEnumerator();
        }

        public override string ToString()
        {
            return _visitedCells.AggregateToString();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public BoggleCell CurrentCell
        {
            get { return _visitedCells.Last; }
        }

        public void Visit(BoggleCell cell)
        {
            _visitedCells.Add(cell);
        }

        public bool HasVisited(BoggleCell cell)
        {
            return _visitedCells.Contains(cell);
        }
    }
}
